const app = getApp();
var api = require('../../api.js');
var mta = require('../../mta_analysis.js');

Page({
  data: {
    list:[],
  },
  //删除功能
  deleteOne:function(e){
    var index = e.currentTarget.dataset.index;
    var newList=this.data.messageList.splice(index, 1);
    this.setData({
      messageList: this.data.messageList
    })
  },
  onLoad: function (options) {
    app.bind();
    this.getMessage();
    mta.Page.init();
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {
    
    app.bind();
    this.getMessage();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };  
  },
  getMessage: function (e) {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url:api.study.comment,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
      },
      success: function (e) {
        console.log(e);
        if (e.data.code == 0) {
          that.setData({
            list: e.data.data
          })
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },
  delete: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(id)
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url:api.study.delComment,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: id,
      },
      success: function (e) {
        console.log(e);
        if (e.data.code == 0) {
          wx.showToast({
            title: '留言删除成功！',
            icon: 'none',
          });
          that.getMessage();
        } else {
          wx.showToast({
            title: '留言删除失败，请重新删除！',
            icon: 'none',
          });
        }
      }
    });
  }
})